package crmdna.group;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;
import javax.inject.Named;
import javax.servlet.http.HttpServletRequest;

import com.google.api.server.spi.config.Api;
import com.google.api.server.spi.config.ApiMethod;
import com.google.api.server.spi.config.ApiMethod.HttpMethod;
import com.google.appengine.api.users.User;

import crmdna.client.Client;
import crmdna.common.APIResponse;
import crmdna.common.APIResponse.Status;
import crmdna.common.Utils;
import crmdna.group.Group.EmailType;
import crmdna.group.Group.GroupProp;

@Api(name = "group")
public class GroupApi {
	@ApiMethod(path = "create", httpMethod = HttpMethod.POST)
	public APIResponse create(@Nullable @Named("client") String client,
			@Named("displayName") String displayName, User user) {

		try {
			if (null == client)
				client = "isha";

			Client.ensureValid(client);
			GroupProp prop = Group.create(client, displayName,
					Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "getAll", httpMethod = HttpMethod.GET)
	public APIResponse getAll(@Nullable @Named("client") String client) {

		try {
			if (null == client)
				client = "isha";

			Client.ensureValid(client);
			List<GroupProp> props = Group.getAll(client);

			return new APIResponse(Status.SUCCESS, props);
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "rename", httpMethod = HttpMethod.GET)
	public APIResponse rename(@Nullable @Named("client") String client,
			@Named("groupId") long groupId,
			@Named("newDisplayName") String newDisplayName, User user) {

		try {
			if (null == client)
				client = "isha";

			Client.ensureValid(client);
			GroupProp prop = Group.rename(client, groupId, newDisplayName,
					Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "delete", httpMethod = HttpMethod.GET)
	public APIResponse delete(@Nullable @Named("client") String client,
			@Named("groupId") long groupId, User user) {

		try {
			if (null == client)
				client = "isha";

			Client.ensureValid(client);
			Group.delete(client, groupId, Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, "Center [" + groupId
					+ "] deleted");
		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "setPaypalApiCredentials", httpMethod = HttpMethod.POST)
	public APIResponse setPaypalApiCredentials(
			@Nullable @Named("client") String client,
			@Named("groupId") long groupId,
			@Named("paypalApiLogin") String login,
			@Named("paypalApiPassword") String pwd,
			@Named("paypalApiSecret") String secret,
			@Named("disable") boolean disable, User user) {

		try {
			if (null == client)
				client = "isha";

			Group.safeGet(client, groupId);

			PaypalApiCredentialsProp prop = Group.setPaypalApiCredentials(
					client, groupId, login, pwd, secret, disable,
					Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "setEmailTemplate", httpMethod = HttpMethod.POST)
	public APIResponse setEmailTemplate(
			@Nullable @Named("client") String client,
			@Named("groupId") long groupId,
			@Named("emailType") EmailType emailType,
			@Named("html") List<String> htmls, User user) {

		try {
			if (null == client)
				client = "isha";

			StringBuilder html = new StringBuilder();
			for (String s : htmls) {
				html.append(s);
			}

			Group.setEmailHtmlTemplate(client, groupId, emailType,
					html.toString(), Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, "success");

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "getEmailTemplates", httpMethod = HttpMethod.GET)
	public APIResponse getEmailTemplates(
			@Nullable @Named("client") String client,
			@Named("groupId") long groupId, HttpServletRequest req) {

		try {
			if (null == client)
				client = "isha";

			Map<EmailType, String> map = new HashMap<>();

			String prefix = req.getScheme() + "://" + req.getServerName() + ":"
					+ req.getServerPort() + "/emailTemplate/get?client="
					+ client + "&groupId=" + groupId + "&emailType=";

			map.put(EmailType.REGISTRATION_CONFIRMATION, prefix
					+ "REGISTRATION_CONFIRMATION");

			map.put(EmailType.REGISTRATION_REMINDER, prefix
					+ "REGISTRATION_REMINDER");

			return new APIResponse(Status.SUCCESS, map);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "getPaypalApiCredentials", httpMethod = HttpMethod.GET)
	public APIResponse getPaypalApiCredentials(
			@Nullable @Named("client") String client,
			@Named("groupId") long groupId, User user) {

		try {
			if (null == client)
				client = "isha";

			Group.safeGet(client, groupId);

			PaypalApiCredentialsProp prop = Group.getPaypalApiCredentials(
					client, groupId, Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "getMandrillConfig", httpMethod = HttpMethod.GET)
	public APIResponse getMandrillConfig(
			@Nullable @Named("client") String client,
			@Named("groupId") long groupId, User user) {

		try {
			if (null == client)
				client = "isha";

			Group.safeGet(client, groupId);

			MandrillConfigProp prop = Group.getMandrillConfig(client, groupId,
					Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}

	@ApiMethod(path = "setMandrillConfig", httpMethod = HttpMethod.POST)
	public APIResponse setMandrillConfig(
			@Nullable @Named("client") String client,
			@Named("groupId") long groupId,
			@Named("mandrillApiKey") String apiKey,
			@Named("mandrillFromEmail") String fromEmail,
			@Named("mandrillFromName") String fromName,
			@Named("disableEmail") boolean disable, User user) {

		try {
			if (null == client)
				client = "isha";

			MandrillConfigProp prop = Group.setMandrillConfig(client, groupId,
					apiKey, fromEmail, fromName, disable,
					Utils.getLoginEmail(user));

			return new APIResponse(Status.SUCCESS, prop);

		} catch (Exception ex) {
			return new APIResponse(ex);
		}
	}
}
